<template>
  <div>
    <el-form ref="theme" :model="themeParams" :rules="rules" label-width="80px">
      <el-divider content-position="left">系统信息</el-divider>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="系统名称" prop="sysName">
            <el-input
              v-model="themeParams.sysName"
              placeholder="请输入系统名称"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="系统版本" prop="sysVersion">
            <el-input
              v-model="themeParams.sysVersion"
              placeholder="请输入系统版本"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"></el-col>
      </el-row>
      <el-divider content-position="left">导航栏</el-divider>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="背景颜色" prop="backgroundColor">
            <el-color-picker v-model="themeParams.backgroundColor"></el-color-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字体颜色" prop="textColor">
            <el-color-picker v-model="themeParams.textColor"></el-color-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider content-position="left">侧边栏</el-divider>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="背景颜色" prop="asideBackgroundColor">
            <el-color-picker v-model="themeParams.asideBackgroundColor"></el-color-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字体颜色" prop="asideTextColor">
            <el-color-picker v-model="themeParams.asideTextColor"></el-color-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="激活颜色" prop="asideActiveColor">
            <el-color-picker v-model="themeParams.asideActiveColor"></el-color-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateTheme">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getDictItemListByCode } from '@/api/dict'
import { addOrUpdateTheme } from '@/api/theme'
export default {
  name: 'AddOrUpdateTheme',
  props: {
    themeParams: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      rules: {
        sysName: [{ required: true, message: '请输入系统名称', trigger: 'blur' }],
        sysVersion: [{ required: true, message: '请输入系统版本', trigger: 'blur' }],
        backgroundColor: [{ required: true, message: '请选择导航栏背景颜色', trigger: 'change' }],
        textColor: [{ required: true, message: '请选择导航栏字体颜色', trigger: 'change' }],
        asideBackgroundColor: [{ required: true, message: '请选择侧边栏背景颜色', trigger: 'change' }],
        asideTextColor: [{ required: true, message: '请选择侧边栏字体颜色', trigger: 'change' }],
        asideActiveColor: [{ required: true, message: '请选择侧边栏激活颜色', trigger: 'change' }]
      },
      defaultColor: []
    }
  },
  created() {
    this.initColor()
  },
  methods: {
    // 查询默认颜色
    initColor() {
      getDictItemListByCode('default_color').then(res => {
        this.defaultColor = res.map(el => el.value)
      })
    },
    // 新增主题
    addOrUpdateTheme() {
      this.$refs['theme'].validate((valid) => {
        if (valid) {
          addOrUpdateTheme(this.themeParams).then(res => {
            // 重置数据
            this.$refs['theme'].resetFields()
            // 父组件查询
            this.$emit('researchThemeList')
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row {
  height: unset !important;
}
</style>
